package com.gitee.qdbp.general.system.biz.personnel.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.general.system.api.personnel.basic.IDepartmentCoreBasic;
import com.gitee.qdbp.general.system.api.personnel.model.DepartmentCoreBean;
import com.gitee.qdbp.general.system.api.personnel.model.DepartmentCoreWhere;
import com.gitee.qdbp.general.system.api.personnel.service.IDepartmentCoreQueryer;
import com.gitee.qdbp.tools.codec.CodeTools;

/**
 * 部门信息业务处理类
 *
 * @author zhh
 * @version 170215
 */
@Service
@Transactional(readOnly = true)
public class DepartmentCoreQueryer implements IDepartmentCoreQueryer {

    /** 部门信息DAO **/
    @Autowired
    private IDepartmentCoreBasic departmentCoreBasic;

    @Override
    public DepartmentCoreBean find(String id) throws ServiceException {
        return departmentCoreBasic.findById(id);
    }

    @Override
    public DepartmentCoreBean find(DepartmentCoreWhere where) throws ServiceException {
        return departmentCoreBasic.find(where);
    }

    @Override
    public PageList<DepartmentCoreBean> list(DepartmentCoreWhere where, OrderPaging paging) throws ServiceException {
        return departmentCoreBasic.list(where, paging);
    }

    @Override
    public PageList<DepartmentCoreBean> list(DepartmentCoreWhere where, OrderPaging paging, boolean parents)
            throws ServiceException {
        PageList<DepartmentCoreBean> depts = departmentCoreBasic.list(where, paging);
        if (parents && depts != null && !depts.isEmpty()) {
            // 查找所有父节点的编号
            Map<String, Boolean> exist = new HashMap<>();
            for (DepartmentCoreBean bean : depts) {
                exist.put(bean.getDeptCode(), true);
            }
            Set<String> parentCodes = new HashSet<>();
            int codeLength = IDepartmentCoreQueryer.DEPT_CODE_LENGTH;
            for (DepartmentCoreBean bean : depts) {
                List<String> pids = CodeTools.split(bean.getDeptCode(), codeLength, false);
                for (String pid : pids) {
                    if (!exist.containsKey(pid)) {
                        parentCodes.add(pid);
                    }
                }
            }
            if (!parentCodes.isEmpty()) {
                // 查询父节点信息
                DepartmentCoreWhere pw = new DepartmentCoreWhere();
                pw.setDeptCodes(new ArrayList<String>(parentCodes));
                OrderPaging pp = new OrderPaging();
                pp.setOrderings(paging.getOrderings());
                pp.setPaging(false);
                pp.setNeedCount(false);
                PageList<DepartmentCoreBean> parent = departmentCoreBasic.list(pw, pp);
                if (parent != null && !parent.isEmpty()) {
                    depts.addAll(parent.toList());
                }
            }
        }
        return depts;
    }

}
